package com.wl156;

import org.acra.ACRA;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.wl156.model.Car;
import com.wl156.model.Certificate;
import com.wl156.model.Good;
import com.wl156.model.User;
import com.wl156.model.WebServiceClient;
import com.wl156.services.MyService;

//@ReportsCrashes(formKey = "", // will not be used
//mailTo = "qhm123+wl156crashes@gmail.com")
public class MyApp extends Application {

	public static final String SHARE_PREFERENCES_SETTINGS = "settings";

	private WebServiceClient mWebServiceClient;
	private Certificate mCurrentCertificate;
	private SharedPreferences mSettingsPreferences;

	private User mCurrentUser;

	public static Certificate sGlobleCertificate;

	public static Good sGoodInfo = new Good();
	public static Car sCarInfo = new Car();

	@Override
	public void onCreate() {
		ACRA.init(this);
		super.onCreate();

		startService(new Intent(this, MyService.class));

		mWebServiceClient = new WebServiceClient(this);
		mSettingsPreferences = getSharedPreferences(SHARE_PREFERENCES_SETTINGS,
				MODE_PRIVATE);
	}

	public WebServiceClient getWebServiceClient() {
		return mWebServiceClient;
	}

	public void setCertificate(String userLoginId, String certificateToken) {
		mCurrentCertificate = new Certificate(userLoginId, certificateToken);
	}

	public Certificate getCertificate() {
		return mCurrentCertificate;
	}

	public Certificate getCertificateFromXml() {
		String userId = mSettingsPreferences.getString("userid", null);
		String token = mSettingsPreferences.getString("token", null);
		String type = mSettingsPreferences.getString("type", null);
		if (userId == null || token == null || type == null) {
			return null;
		} else {
			return new Certificate(userId, token, type);
		}
	}

	public int getCountPerPage() {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(this);
		return Integer.valueOf(sp.getString("pager", "10"));
	}

	public boolean isLogined() {
		Certificate cert = getCertificateFromXml();
		if (cert != null) {
			sGlobleCertificate = new Certificate();
			sGlobleCertificate.certifcateToken = cert.certifcateToken;
			sGlobleCertificate.userLoginId = cert.userLoginId;
		}

		return getCertificateFromXml() == null ? false : true;
	}

	public void login(String username, String token, String type) {
		setCertificate(username, token);
		mSettingsPreferences.edit().putString("userid", username)
				.putString("token", token).putString("type", type).commit();
		sGlobleCertificate = new Certificate();
		sGlobleCertificate.certifcateToken = token;
		sGlobleCertificate.userLoginId = username;
	}

	public void logout() {
		mCurrentCertificate = null;
		mSettingsPreferences.edit().putString("userid", null)
				.putString("token", null).putString("type", null).commit();
	}

	public void setCurrentUser(User user) {
		mCurrentUser = user;
	}

	public User getCurrentUser() {
		return mCurrentUser;
	}

	public void putUserName(String username) {
		mSettingsPreferences.edit().putString("username", null).commit();
	}

	public String getUserNameFromXml() {
		return mSettingsPreferences.getString("username", null);
	}

	public String getUserIdFromXml() {
		return mSettingsPreferences.getString("userid", null);
	}
}
